// miniprogram/pages/tabBar/find.js
let isend = false // 是否是最后一页
let selectDate = '' // 当前的日期
let heightArr = []
let newHeightArr = []
let distanceTop = ''

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    selectYear: '', // 当前年份
    selectMonth: '',  // 当前月份
    isEmpty: false,
    date: '2020-07',
    startDate: '2012-10',
    endDate: '',
    isLoading: false,
    arrObj: [
      { id: 0, name: '阅读', isRefresh: false, type: 'read' },
      { id: 1, name: '图文', isRefresh: false, type: 'pic' },
      { id: 2, name: '文章', isRefresh: false, type: 'music' },
      // { id: 3, name: '专题', isRefresh: false, type: 'special' },
      // { id: 4, name: '阅读', isRefresh: false, type: '' },
      // { id: 5, name: '热榜', isRefresh: false, type: '' },
      // { id: 6, name: '问答', isRefresh: false, type: '' },
    ],
    currentTab: 1,
    newDate: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const newDate = this.getNewDate()
    wx.getSystemInfo({
      success: function (res) {
        distanceTop = res.windowHeight - 100
      }
    })
    selectDate = newDate.YM
    this.setData({
      newDate
    })
    let currentTab = this.data.currentTab
    let arrObj = this.data.arrObj

    console.log('arrObj[currentTab].type', arrObj[currentTab].type)
      this.selectComponent('#' + arrObj[currentTab].type + 'list').gethp(arrObj[currentTab].type)
      this.selectComponent('#tabs').changeSort(currentTab)
    
  },
  tabchange(event) {
    console.log(event)
    let index = event.detail.id
    let item = this.data.arrObj[index]
    if (!item.isRefresh) {
      this.setData({
        ['arrObj[' + index + '].isRefresh']: true
      })
      if (item.type == 'read' || item.type == 'pic' || item.type == 'music') {
        this.selectComponent('#' + item.type + 'list').gethp(item.type)
      }
    }
    this.setData({
      currentTab: index
    })
  },

  switchTab(event) {
    console.log(event)
    let index = event.detail.current
    let item = this.data.arrObj[index]
    if (!item.isRefresh) {
      this.setData({
        ['arrObj[' + index + '].isRefresh']: true
      })
      if (item.type == 'read' || item.type == 'pic' || item.type == 'music') {
        this.selectComponent('#' + item.type + 'list').gethp(item.type)
      }
    }
    this.setData({
      currentTab: index
    })
    this.selectComponent('#tabs').changeSort(index)
  },

  // 获取当前月份
  getNewDate() {
    const newDate = new Date()
    const year = newDate.getFullYear()
    let month = this.formatNumber(newDate.getMonth() + 1)
    const YM = year + '-' + month
    return {
      YM,
      year,
      month
    }
  },

  formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  },

  onShow: function() {

  },

  onHide: function() {

  },

  onUnload: function() {

  },

  onPullDownRefresh: function() {

  },

  onReachBottom: function() {

  },

  onShareAppMessage: function() {

  }
})